#include <stdio.h>
#include <string.h>

int main()
{
    char buffer[50];
    int num = 123;
    char str[] ="abc";
    /*将整数num和字符串str格式化为一个新的字符串存入buffer*/
    int len = snprintf(buffer,sizeof(buffer),"This number is %d and the string is %s",num,str);
    if(len < 0)
    {
        //发生错误
        printf("Error occurred during snprintf.\n");
    }else if(len >=sizeof(buffer))
    {
        //数据被截断
        printf("Data was truncated.\n");
    }else{
        //正常情况，输出格式休后的字符串
        printf("%s\n ",buffer);
    }

    char buffer1[10];
    int len2 = snprintf(buffer1,sizeof(buffer1)-1,"abcd");
    printf("%s %d\n",buffer1,len2);

    len2 = snprintf(buffer1+len2,sizeof(buffer1)-1,"ef");
    printf("%s %d\n",buffer1,len2);
    
    return 0;
}